rust-bindgen
A native binding generator for the Rust language.
rust-bindgen was originally ported from clay's bindgen.
Requirements
- Clang >= 3.5
Installing
$ cargo install bindgen
Bindgen will be dynamically linked to your default clang version. See
clang-sys if you
want to use an other version or do a static link build. The clang-sys feature
static
can be activated via the bindgen
feature clang_sys/static
.
Usage
Command Line
$ bindgen <header> [<bindgen options>] [-- <clang options>]
See --help
for a list of the supported options.
Plugin
bindgen!
The use of this plugin requires the use of a nightly compiler.
Options:
Option Name | Type | Default |
---|---|---|
link | str | |
match | str | |
builtins | bool | true |
allow_unknown_types | bool | false |
clang_args | str |
Examples
Generate a Lua binding with the CLI
bindgen --link lua --builtins /usr/include/lua.h -o lua.rs
Generate a Lua binding with the plugin
Cargo.toml
= "*"
bindgen
main.rs
Using a build script to generate bindings at compile time
Due to a known issue with include!
(https://github.com/rust-lang/rfcs/issues/752) when generating
bindings in a build script and importing them with include!
, you'll want to wrap the bindings
in a module before writing them to a file to avoid triggering the issue with top-level
attributes in include!
. Some more discussion about this issue can be found
here.
Cargo.toml
...
name = "bindgen_ex"
build = "build.rs"
bindgen = "0.19.0"
build.rs
extern crate bindgen;
use *;
use File;
main.rs
include!;